home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / repeat < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  3.0 KB  |  123 lines

  1. #!/bin/ksh
  2. # repeat: repeat a command.
  3. # @(#) repeat.ksh 1.1 93/06/03
  4. # 90/05 john h. dubois iii (john@armory.com)
  5. # 90/11 added help
  6. # 93/06/03 Added s, h, p, and v options
  7.  
  8. alias istrue="test 0 -ne"
  9. alias isfalse="test 0 -eq"
  10.  
  11. name=${0##*/}
  12. Usage=\
  13. "Usage: repeat [-hpv] [-s <sec>] [[startcount]-][endcount] command [arg ...]"
  14.  
  15. typeset -i count=1 forever=0 sleep=0 print=0 verbose=0
  16.  
  17. while getopts :0123456789hpvs: opt; do
  18.     case $opt in
  19.     h)
  20.     echo \
  21. "$name: repeatedly execute a command line.
  22. $Usage
  23. commandline is executed once for each integer from startcount through endcount
  24. inclusive.  The default for startcount is 1 if a positive endcount or no
  25. endcount is given, and -1 if a negative endcount is given.  A count
  26. parameter consisting of a single number is taken to be an endcount.  If
  27. only an endcount is given and it is positive, commandline is executed
  28. endcount times.  endcount may be less than startcount.  If no endcount is
  29. given (e.g. a count parameter of \"10-\"), commandline execution repeats
  30. indefinitely with the iteration variable incrementing in a positive
  31. direction.  A count parameter of consisting of \"-\" will repeat
  32. indefinitely starting with 1.
  33.  
  34. Note that quoting and variables in commandline are interpreted twice, once
  35. when it is passed to the repeat command, and once when it is actually executed.
  36.  
  37. The iteration variable is \"count\".  If \$count is used in commandline, make
  38. sure it is quoted with ' or \.
  39.  
  40. Options:
  41. -h: Print this help.
  42. -p: Print value of iteration variable on stderr before each iteration.
  43. -s <sec>: sleep for <sec> seconds after each iteration except the last.
  44. -v: Print start and end values before beginning."
  45.        exit 0
  46.        ;;
  47.     s)
  48.     sleep=$OPTARG || exit 1
  49.     ;;
  50.     p)
  51.     print=1;;
  52.     v)
  53.     verbose=1;;
  54.     [0-9])
  55.     break;;
  56.     +?)
  57.     echo "$name: options should not be preceded by a '+'."
  58.     exit 1
  59.     ;;
  60.     :) 
  61.     print -r -u2 -- \
  62.     "$name: Option '$OPTARG' requires a value.  Use -h for help."
  63.     exit 1
  64.     ;;
  65.     ?) 
  66.     echo "$name: $OPTARG: bad option.  Use -h for help."
  67.     exit 1
  68.     ;;
  69.     esac
  70. done
  71.  
  72. # remove args that were options
  73. let OPTIND=OPTIND-1
  74. shift $OPTIND
  75.  
  76. if [ $# -lt 2 ]; then
  77.     echo "$Usage\nUse -h for help."
  78.     exit 1
  79. fi
  80.  
  81. case "$1" in
  82. ?(-)+([0-9]))
  83.     end=$1
  84.     [[ $end = -* ]] && count=-1
  85.     ;;
  86. ?(-)+([0-9])-)
  87.     # Start value only
  88.     count=${1%-}
  89.     forever=1
  90.     ;;
  91. ?(-)+([0-9])-?(-)+([0-9]))
  92.     # Start and end value
  93.     end=${1##?(-)+([0-9])-}
  94.     count=${1%-$end}
  95.     ;;
  96. -)
  97.     forever=1
  98.     ;;
  99. *) 
  100.     print -u2 "Bad count parameter."
  101.     exit 1
  102.     ;;
  103. esac
  104.  
  105. shift
  106.  
  107. [ -z "$end" -o count -le "$end" ] && increment=1 || increment=-1
  108.  
  109. istrue verbose && print -u2 "start=$count end=$end"
  110.  
  111. # Need to do this here so that up to this point, -0 will keep the leading -
  112. # and end will not be 0 if no value assigned
  113. typeset -i end
  114.  
  115. let end+=increment    # make loop inclusive of original endcount
  116.  
  117. while istrue forever || [ $count -ne $end ]; do
  118.     istrue print && print -u2 -- $count
  119.     eval "$@"
  120.     istrue sleep && sleep $sleep
  121.     let count+=increment
  122. done
  123.